package cn.iocoder.yudao.module.warehouseoperationlog.service.warehouseoperationlog;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.module.warehouseoperationlog.dal.mysql.warehouseoperationlog.WarehouseOperationLogMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.warehouseoperationlog.controller.admin.warehouseoperationlog.vo.*;
import cn.iocoder.yudao.module.warehouseoperationlog.dal.dataobject.warehouseoperationlog.WarehouseOperationLogDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;


import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.warehouseoperationlog.enums.ErrorCodeConstants.*;


/**
 * 库房操作记录表（记录库房相关操作，支撑审计追溯） Service 实现类
 *
 * @author wwwhk
 */
@Service
@Validated
public class WarehouseOperationLogServiceImpl implements WarehouseOperationLogService {

    @Resource
    private WarehouseOperationLogMapper warehouseOperationLogMapper;

    @Override
    public Long createWarehouseOperationLog(WarehouseOperationLogSaveReqVO createReqVO) {
        // 插入
        WarehouseOperationLogDO warehouseOperationLog = BeanUtils.toBean(createReqVO, WarehouseOperationLogDO.class);
        warehouseOperationLogMapper.insert(warehouseOperationLog);

        // 返回
        return warehouseOperationLog.getId();
    }

    @Override
    public void updateWarehouseOperationLog(WarehouseOperationLogSaveReqVO updateReqVO) {
        // 校验存在
        validateWarehouseOperationLogExists(updateReqVO.getId());
        // 更新
        WarehouseOperationLogDO updateObj = BeanUtils.toBean(updateReqVO, WarehouseOperationLogDO.class);
        warehouseOperationLogMapper.updateById(updateObj);
    }

    @Override
    public void deleteWarehouseOperationLog(Long id) {
        // 校验存在
        validateWarehouseOperationLogExists(id);
        // 删除
        warehouseOperationLogMapper.deleteById(id);
    }

    @Override
    public void deleteWarehouseOperationLogListByIds(List<Long> ids) {
        // 删除
        warehouseOperationLogMapper.deleteByIds(ids);
    }


    private void validateWarehouseOperationLogExists(Long id) {
        if (warehouseOperationLogMapper.selectById(id) == null) {
            throw exception(WAREHOUSE_OPERATION_LOG_NOT_EXISTS);
        }
    }

    @Override
    public WarehouseOperationLogDO getWarehouseOperationLog(Long id) {
        return warehouseOperationLogMapper.selectById(id);
    }

    @Override
    public PageResult<WarehouseOperationLogDO> getWarehouseOperationLogPage(WarehouseOperationLogPageReqVO pageReqVO) {
        return warehouseOperationLogMapper.selectPage(pageReqVO);
    }

    @Override
    public PageResult<WarehouseOperationLogWithWarehouseRespVO> getWarehouseOperationLogWithWarehousePage(WarehouseOperationLogPageReqVO pageReqVO) {
        // 1. 创建 MyBatis-Plus 分页对象（从请求参数中获取页码和每页条数）
        Page<WarehouseOperationLogWithWarehouseRespVO> page = Page.of(pageReqVO.getPageNo(), pageReqVO.getPageSize());

        // 2. 调用 Mapper 方法，传入分页对象和查询条件
        IPage<WarehouseOperationLogWithWarehouseRespVO> resultPage = warehouseOperationLogMapper.selectPageWithWarehouse(page, pageReqVO);

        // 3. 转换为框架要求的 PageResult 返回
        return new PageResult<>(
                resultPage.getRecords(),  // 分页数据列表
                resultPage.getTotal()     // 总记录数
        );
    }
}